package controllers;
import models.Email;


import org.apache.commons.mail.EmailException;


import java.util.ArrayList;
import java.util.List;

import play.data.validation.Required;
import play.mvc.Controller;
import models.FilterByPriority;
import models.Idea;
import models.SearchIdeaResults;
import models.User;


public class SearchForIdeas extends Controller
{
	
	//public static List<Idea> finalIdeas = new ArrayList<Idea>();
	/**Method to be called afterwards in the navigation
	 * of the view
	 * 
	 * @Author Mohamed Ali
	 */
	public static void searchIndex(){
		render();
	}
	/**Method to call searchForIdeas Method that does the whole search
	 * and shows results of the search as well as the search word entered after taking input from 
	 * the user  . It is also to be used afterwards in the naviagtion of the view.
	 * 
	 * 
	 * @Author Mohamed Ali
	 */
	public static void searchIdeas()
	{
		String search = params.get("search");
		SearchIdeaResults results = Idea.searchForIdeas(search);
		List<Idea> ideasTwo=results.ideas;
		//finalIdeas= ideas2;
		//System.out.println("num: "+finalIdeas.size());
		render(results, ideasTwo ,search);
	}
	/**Method to call searchForIdeas Method that does the whole search
	 * and shows results of the search as well as the search rate entered after taking input from 
	 * the user  . It is also to be used afterwards in the naviagtion of the view.
	 * 
	 * 
	 * @Author Mohamed Ali
	 */
	public static void searchIdeasByRate()
	{
		String searchOne=params.get("searchOne");
		double rate=Integer.parseInt(searchOne);
		SearchIdeaResults results=Idea.searchForIdeasByRate(rate);
		List<Idea> ideasTwo=results.ideas;
		render(results, ideasTwo ,searchOne);
		
	}
	/**Method to be called afterwards in navigation
	 * of the view to show the ideas
	 * @params id
	 * @Author Mohamed Ali
	 */
	public static void showIdeas(long id)
	{
		Idea idea= Idea.findById(id);
		render(idea);
	}
	
	public static void showIdeasByRate(long id)
	{
		Idea idea= Idea.findById(id);
		render(idea);
	}
	/** Method that takes the name of the idea that the user thinks has a duplicate
	 * and then takes from the text box the name that the user thinks is duplicate to this
	 * idea and puts them into a string and then sends this string in a mail to the organizer
	 * @param IdOfIdea
	 * @param duplicate
	 * @throws EmailException
	 * @author Mohamed Ali
	 */
	public static void sendDuplicate(long ideaId,String duplicate) throws EmailException
	{
		Idea idea = Idea.findById(ideaId);
		System.out.println(ideaId+"   "+duplicate);
		String IdeaTitle=idea.getTitle();
		String body = " ' "+" "+ IdeaTitle +" ' and ' "+" "+duplicate+" ' ";
		
		
		String idString=session.get("userId");
		Long idOne=Long.parseLong(idString);
		User user= User.findById(idOne);
		
		String source = user.email;
		String destination = idea.organization.lead.email;
		System.out.println(idea.organization.lead.email);
		Email email = new Email(source,destination ,
		"Request for duplication ",
		body);
			
		email.sendEmail();
		render(email,body,idea);
	}
	
}
